package com.mousycoder.mysql.controller;

import com.google.common.collect.Lists;
import com.mousycoder.mysql.constant.RestItemResult;
import com.mousycoder.mysql.dao.UserDao;
import com.mousycoder.mysql.domain.User;
import com.mousycoder.mysql.persist.mapper.custom.CustomUserMapper;
import com.mousycoder.mysql.persist.mapper.generated.UserMapper;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 批量插入千万数据到 mysql ，分批去插，建议提高连接数，以及每次插入数据量不要超过1W，这样速度最快。
 *
 * @author mousycoder
 * @version 1.0
 * @date 2022/3/22 8:36 PM
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserDao userDao;

    @Resource
    private UserMapper userMapper;

    @Resource
    private CustomUserMapper customUserMapper;


    @RequestMapping("/query")
    private RestItemResult<User> queryById() {
        RestItemResult<User> userRestItemResult = new RestItemResult<>();
        User locate = userDao.locate(1L);
        userRestItemResult.setItem(locate);
        return userRestItemResult;
    }


    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    private RestItemResult<com.mousycoder.mysql.model.User> get(@PathVariable Long id) {
        RestItemResult<com.mousycoder.mysql.model.User> userRestItemResult = new RestItemResult<>();
        com.mousycoder.mysql.model.User user = userMapper.selectByPrimaryKey(id);
        userRestItemResult.setItem(user);
        return userRestItemResult;
    }

    @RequestMapping(value = "/search",method = RequestMethod.GET)
    private RestItemResult<com.mousycoder.mysql.model.User> get(String name,String company) {
        RestItemResult<com.mousycoder.mysql.model.User> userRestItemResult = new RestItemResult<>();
        com.mousycoder.mysql.model.User user = customUserMapper.selectByNameAndcompany(name,company);
        userRestItemResult.setItem(user);
        return userRestItemResult;
    }

    @RequestMapping(value = "/batchInsert",method = RequestMethod.GET)
    private RestItemResult<com.mousycoder.mysql.model.User> batchInsert() {
        RestItemResult<com.mousycoder.mysql.model.User> userRestItemResult = new RestItemResult<>();
        List<com.mousycoder.mysql.model.User> userList = new ArrayList<>();
        int num = 10000000;
        for (int i = 0; i < num; i++) {
            com.mousycoder.mysql.model.User user = new com.mousycoder.mysql.model.User();
            user.setName("mousycoder"+i);
            user.setCompany("mousycoder"+i);
            user.setAge(Long.valueOf(i));
            user.setLevel(Long.valueOf(i));
            userList.add(user);
        }
        System.out.println("test");

        List<List<com.mousycoder.mysql.model.User>> partition = Lists.partition(userList, 10000);
        partition.forEach(users -> {customUserMapper.batchInsert(users);
            System.out.println("test");
        });
        userRestItemResult.setResult("1");
        return userRestItemResult;
    }
}
